﻿using River.Core;
using River.IntAI.ISdk;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace River.IntAI
{
    public partial class FrmSynthesis : BaseForm
    {
        string destFile = null;// 目标文件
        bool showShowDetail = false;/// 显示详细信息标志
        string[] srcFiles = null;// 待处理文件列表
        int cur_file_index = 0;// 当前处理文件序号
        StringBuilder sb = new StringBuilder();
        OcrUtils ocrUtils = new OcrUtils();
        int MAX_LENGTH = 1024;
        public FrmSynthesis()
        {
            InitializeComponent();
            showShowDetail = checkShowDetail.Checked;
            initCb(cbspd, 5, 9);//语速，取值0-9，默认为5中语速 否
            initCb(cbpit, 5, 9);//音调，取值0-9，默认为5中语调 否
            initCb(cbvol, 5, 15);//音量，取值0-15，默认为5中音量 否

            // 读取配置
            cbspd.Text = IniUtil.INIGetStringValue(IniSection.Synthesis, IniKey.spd, "5");
            cbpit.Text = IniUtil.INIGetStringValue(IniSection.Synthesis, IniKey.pit, "5");
            cbvol.Text = IniUtil.INIGetStringValue(IniSection.Synthesis, IniKey.vol, "5");
            cbper.Text = IniUtil.INIGetStringValue(IniSection.Synthesis, IniKey.per, "");
            rtbContent.Text = IniUtil.INIGetStringValue(IniSection.Synthesis, IniKey.txt, "");
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            rtbContent_TextChanged(null, null);
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            IniUtil.INIWriteValue(IniSection.Synthesis, IniKey.spd, cbspd.Text);
            IniUtil.INIWriteValue(IniSection.Synthesis, IniKey.pit, cbpit.Text);
            IniUtil.INIWriteValue(IniSection.Synthesis, IniKey.vol, cbvol.Text);
            IniUtil.INIWriteValue(IniSection.Synthesis, IniKey.per, cbper.Text);
            IniUtil.INIWriteValue(IniSection.Synthesis, IniKey.txt, rtbContent.Text);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            ReqSynthesis req = new ReqSynthesis();
            req.tex = rtbContent.Text;
            req.spd = GetCbValue(cbspd, 5, 9);
            req.pit = GetCbValue(cbpit, 5, 9);
            req.vol = GetCbValue(cbvol, 5, 15);
            int per = 0;
            string strper = cbper.Text;
            switch (strper)
            {
                case "女声-度小美":
                    break;
                case "男声-度小宇":
                    per = 1;
                    break;
                case "情感合成-度逍遥":
                    per = 3;
                    break;
                case "情感合成-度丫丫":
                    per = 4;
                    break;
            }
            req.per = per;
            destFile = Utils.SaveFileName("*mp3音频|*.mp3", string.Format("{0}.mp3", DateTime.Now.ToString("yyMMddHHmmss")));
            if (string.IsNullOrEmpty(destFile)) return;
            if (SpeechUtils.Synthesis(req, destFile, onSynthesisEvent))
            {
                System.Diagnostics.Process.Start(destFile);// 打开文件
            }
        }

        private void onSynthesisEvent(string msg)
        {
            ShowStatus(msg);
        }
        /// <summary>
        /// 显示状态信息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="skiptime"></param>
        private void ShowStatus(string msg, bool checkShow = false, bool skiptime = false)
        {
            if (checkShow && !showShowDetail) return;
            this.InvokeDele(new Action(() =>
            {
                rtbStatus.AppendText((skiptime ? "" : Utils.NowTime() + ": ") + msg + "\r\n");
                rtbStatus.Select(rtbStatus.Text.Length, 0);
                rtbStatus.ScrollToCaret();
            }));
        }

        /// <summary>
        /// 清空状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEmptyStatus_Click(object sender, EventArgs e)
        {
            this.InvokeDele(new Action(() =>
            {
                rtbStatus.Text = string.Empty;
            }));
        }

        private void checkShowDetail_CheckedChanged(object sender, EventArgs e)
        {
            showShowDetail = checkShowDetail.Checked;
        }

        private void rtbContent_TextChanged(object sender, EventArgs e)
        {
            this.InvokeDele(new Action(() =>
            {
                int length = rtbContent.Text.Length;
                bool enableSynthesis = length > 0 && length < MAX_LENGTH;
                lbWordNum.Text = string.Format("{0} / {1}", length, MAX_LENGTH);
                btnStart.Enabled = enableSynthesis;
                lbWordNum.ForeColor = enableSynthesis ? Color.Green : Color.Red;
            }));
        }
        /// <summary>
        /// 清空输入内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEmptyContent_Click(object sender, EventArgs e)
        {
            this.InvokeDele(new Action(() =>
            {
                rtbContent.Text = string.Empty;
            }));
        }
        // 读取下拉框选中值
        private int GetCbValue(ComboBox cb, int defaultValue, int max = 0, int min = 0)
        {
            int value = 0;
            if (!int.TryParse(cb.Text, out value) || value > max || value < min)
                value = defaultValue;

            return value;
        }
        // 初始化
        private void initCb(ComboBox cb, int defaultValue, int max = 0, int min = 0)
        {
            cb.Items.Clear();
            for (int i = min; i <= max; i++)
            {
                cb.Items.Add(i);
            }
            cb.Text = defaultValue.ToString();
            cb.EndUpdate();
        }
    }
}
